package com.lyz.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lyz.auth.application.convert.AuthRolePermissionDtoConverter;
import com.lyz.auth.application.dto.AuthRolePermissionDto;
import com.lyz.auth.common.entity.Result;
import com.lyz.auth.domain.entity.AuthRolePermissionBo;
import com.lyz.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色权限controller
 *
 * @author: YizeLee
 * @date: 2024/5/2
 */
@RestController
@RequestMapping("/rolePermission/")
@Slf4j
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限关联关系
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDto authRolePermissionDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authRolePermissionDto));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDto.getPermissionIdList()), "权限关联不能为空");
            Preconditions.checkNotNull(authRolePermissionDto.getRoleId(), "角色不能为空");
            AuthRolePermissionBo authRolePermissionBo = AuthRolePermissionDtoConverter.INSTANCE.convertDtoToBo(authRolePermissionDto);
            return Result.ok(authRolePermissionDomainService.add(authRolePermissionBo));
        } catch (Exception e) {
            log.error("PermissionController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色权限失败");
        }
    }

}
